package com.qiangesoft.cloud.seata.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.cloud.seata.mapper.StockMapper;
import com.qiangesoft.cloud.seata.model.Stock;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

@Service
public class StockService extends ServiceImpl<StockMapper, Stock> {

    @GlobalTransactional
    public int decNum(JSONObject jsonObject) {
        Stock stock = baseMapper.selectById(1);
        stock.setNum(stock.getNum() - 1);
        int update = baseMapper.updateById(stock);

        // 发生异常
//        int i = 10 / 0;

        return update;
    }
}
